/*
 * Created on 16/08/2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package br.com.site.model.entity;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.OneToOne;

import br.com.site.model.dao.DAOException;
import br.com.site.model.dao.DAOFactory;
import br.com.site.model.dao.FotoUsuarioDAO;

/**
 * @author pgr-eduardo
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
@Entity
public class FotoUsuario extends ArquivoManager implements
		CrudBaseEntity<FotoUsuario, Long>, Auditoria {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@OneToOne(mappedBy = "foto")
	private Usuario usuario;

	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	@Override
	public void remove() throws BusinessException {
		DAOFactory factory = DAOFactory.getInstance();
		try {
			factory.txBegin();
			FotoUsuarioDAO dao = factory.getDao(FotoUsuarioDAO.class);
			dao.remove(this.getId());

			factory.txCommit();
		} catch (DAOException e) {
			factory.txRollback();
			throw new BusinessException(e);
		} finally {
			factory.shutdown();
		}

	}

	@Override
	public void save() throws BusinessException {

		DAOFactory factory = DAOFactory.getInstance();
		try {
			factory.txBegin();
			FotoUsuarioDAO dao = factory.getDao(FotoUsuarioDAO.class);
			dao.save(this);

			factory.txCommit();
		} catch (DAOException e) {
			factory.txRollback();
			throw new BusinessException(e);
		} finally {
			factory.shutdown();
		}
	}

	public static FotoUsuario find(Long oid) {
		DAOFactory factory = DAOFactory.getInstance();
		FotoUsuarioDAO dao = factory.getDao(FotoUsuarioDAO.class);

		return dao.find(oid);
	}

	public static List<FotoUsuario> find() {
		DAOFactory factory = DAOFactory.getInstance();
		FotoUsuarioDAO dao = factory.getDao(FotoUsuarioDAO.class);

		return dao.find();
	}

	public static List<FotoUsuario> findByCriteria(FotoUsuario avisoMural) {
		DAOFactory factory = DAOFactory.getInstance();
		FotoUsuarioDAO dao = factory.getDao(FotoUsuarioDAO.class);

		return dao.findByCriteria(avisoMural);
	}

	public static List<FotoUsuario> find(int first, int max) {
		DAOFactory factory = DAOFactory.getInstance();
		FotoUsuarioDAO dao = factory.getDao(FotoUsuarioDAO.class);

		return dao.find(first, max);
	}

	public static List<FotoUsuario> findByCriteria(FotoUsuario avisoMural,
			int first, int max) {
		DAOFactory factory = DAOFactory.getInstance();
		FotoUsuarioDAO dao = factory.getDao(FotoUsuarioDAO.class);

		return dao.findByCriteria(avisoMural, first, max);

	}

}